import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';

late TextEditingController searchTextController;

class QuickSearchBooksWidget extends StatefulWidget {
  @override
  State<QuickSearchBooksWidget> createState() => _QuickSearchBooksWidgetState();
}

class _QuickSearchBooksWidgetState extends State<QuickSearchBooksWidget> {
  final BooksController _booksController = Get.find();

  @override
  void initState() {
    super.initState();
    searchTextController = TextEditingController(text: '');
  }

  @override
  void dispose() {
    searchTextController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 45.0,
      child: Padding(
        padding: const EdgeInsets.all(2.0),
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 6.0),
          child: TextField(
            onSubmitted: (query) {
              if (query.trim() != '') {
                _booksController.openLibraryReadingBookItems.clear();
                _booksController.selectedLibraryReadingBookItems.clear();
                BookSearchDto dto = new BookSearchDto();
                dto.query = query;
                _booksController.fetchSearchOpenLibraryBookItems(0, 0, dto);
              } else {
                _booksController.fetchOpenReadingItems(Constants.pageLimit, 0);
              }
            },
            style: TextStyle(
                color: Color.fromRGBO(254, 254, 254, 1), fontSize: 14),
            controller: searchTextController,
            decoration: InputDecoration(
              hintText: 'Search Books...',
              hintStyle: TextStyle(color: Get.theme.primaryColorLight),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.white, width: 1.0),
                borderRadius: BorderRadius.circular(20.0),
              ),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(
                    color: Get.theme.primaryColorLight.withOpacity(0.6),
                    width: 1.0),
                borderRadius: BorderRadius.circular(20.0),
              ),
              suffixIcon: IconButton(
                icon: Icon(
                  Icons.clear,
                  color: Get.isDarkMode
                      ? Get.theme.primaryColorLight
                      : Colors.black.withOpacity(0.4),
                ),
                onPressed: () {
                  searchTextController.clear();
                  _booksController.selectedLibraryReadingBookItems.clear();
                  _booksController.openLibraryReadingBookItems.clear();
                  _booksController.fetchOpenReadingItems(
                      Constants.pageLimit, 0);
                },
              ),
              prefixIcon: IconButton(
                icon: Icon(Icons.search, color: Colors.white),
                onPressed: () {},
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(20.0),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
